/*
 * 文件名: SingleSelectDialogEx.java
 * 版    权： Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-LIJINHUA001
 * 创建时间:2012-1-16
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.dialog;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import app.widget.BaseDialog;

import com.longevityassistant.activity.R;

/**
 * [列表选择对话框]<BR>
 * [扩展了SingleSelectDialog数据格式的内容]
 * 
 * @author EX-LIJINHUA001
 * @version [Android PABank C01, 2012-1-16]
 */
public class SingleSelectDialogEx extends BaseDialog {

	/**
	 * 显示的key
	 */
	public static final String SHOW_KEY	= "SingleSelectDialogEx_ShowKey";
	
	/**
	 * ListView
	 */
	private ListView listView;

	/**
	 * 上下文
	 */
	private Context context;

	/**
	 * 选项集合
	 */
	private List<Map<String, Object>> list;

	/**
	 * 返回View
	 */
	private View contentView;

	/**
	 * 显示值对应的KEY
	 */
	private String showKey;

	/**
	 * 回调监听
	 */
	private OnItemSelectListener onItemSelectListener;

	/**
	 * 
	 * [构造简要说明]
	 * @param context 上下文
	 * @param title 标题
	 * @param list 列表数据
	 * @param showKey 需要显示内容的key
	 */
	public SingleSelectDialogEx(Context context, String title,
			List<Map<String, Object>> list, String showKey) {
		super(context);
		this.context = context;

		this.list = list;
		this.showKey = showKey;

		setTitle(title);

		initView();
	}

	private void initView() {
		contentView = LayoutInflater.from(context).inflate(
				R.layout.app_widget_dialog_select, null);

		listView = (ListView) contentView.findViewById(R.id.list);

		listView.setDivider(null);
		listView.setCacheColorHint(0x00000000);

		listView.setVerticalScrollBarEnabled(false);

		listView.setAdapter(new DialogAdapter(context, list));

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, Object> map = list.get(position);
				if (onItemSelectListener != null) {
					onItemSelectListener.onItemSelect(map);
				}
				SingleSelectDialogEx.this.dismiss();
			}

		});

		setBtn1Visible(false);
		setBtn2Visible(false);
	}

	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @return
	 * @see com.pingan.pabank.widget.BaseDialog#createContentView()
	 */
	@Override
	public View createContentView() {
		// TODO Auto-generated method stub
		return contentView;
	}

	class DialogAdapter extends BaseAdapter {
		/**
		 * LayoutInfalter
		 */
		private LayoutInflater inflater;
		/**
		 * 选项集合
		 */
		private List<Map<String, Object>> list;
		/**
		 * 缓存对象
		 */
		ViewHolder holder;

		/**
		 * 
		 * [构造简要说明]
		 * 
		 * @param context
		 *            上下文
		 * @param list
		 *            集合
		 */
		public DialogAdapter(Context context, List<Map<String, Object>> list) {
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {

			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.app_widget_dialog_select_item,
						null);
				holder.textView = (TextView) convertView
						.findViewById(R.id.textView);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			Map<String, Object> map = list.get(position);

			holder.textView.setText(map.get(showKey).toString());

			return convertView;
		}

		class ViewHolder {
			TextView textView;
		}

	}

	/**
	 * 
	 * 选择完成回调接口
	 * 
	 * @author EX-LIJINHUA001
	 * @version [Android PABank C01, 2012-1-16]
	 */
	public interface OnItemSelectListener {
		public void onItemSelect(Map<String, Object> selectItem);
	}
	
	/**
	 * 设置选择Item后监听器
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param onItemSelectListener
	 */
	public void setOnItemSelectListener(
			OnItemSelectListener onItemSelectListener) {
		this.onItemSelectListener = onItemSelectListener;
	}
}
